﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.ComponentModel;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using BusinessManagementStudio.Models.BusinessObject.Reports;
using BusinessManagementStudio.Models.BusinessObject.Services;
using BusinessManagementStudio.Utilities;
using BusinessManagementStudio.ViewModel;

namespace BusinessManagementStudio.Controllers
{
    public class ReportController : Controller
    {
        private InvoiceService invs = new InvoiceService();
        private OrderService os = new OrderService();
        private QuotationService qs = new QuotationService();
        private ReportService rptService = new ReportService();
        private ContractorAssignmentService conts = new ContractorAssignmentService();
        //
        // GET: /ReportSelection/
        [HttpGet]
        public ActionResult ReportSelection(String id)
        {
            ReportView rptView = new ReportView();

            if (id == Configuration.InvoiceReportName)
                return RedirectToAction("InvoiceReportViewer");
            else if (id == Configuration.QuotationReportName)
                return RedirectToAction("QuotationReportViewer");
            else if (id == Configuration.WorkOrderReportName)
                return RedirectToAction("WorkOrderReportViewer");

            return View();
        }

        public ActionResult GetInvoiceData(int start, int limit)
        {
            IEnumerable<IData> dtoArray = rptService.GetAllInvoices(invs.GetAllInvoices());   
            return Json(new { data = dtoArray }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Reports()
        {
            ReportView rptView = new ReportView();

            return View(rptView);
        }
        
        public JsonResult UpdateInvoiceExtraInfo(Invoice model)
        {
            // Update model to your db
            string message = "Success";

            invs.Update(model);

            return Json(message, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateWORpt(ContractorAssignment model)
        {
            // Update model to your db
            string message = "Success";

            conts.Update(model);

            return Json(message, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateQuotationRpt(Quotation model)
        {
            // Update model to your db
            string message = "Success";

            qs.Update(model);

            return Json(message, JsonRequestBehavior.AllowGet);
        }

        public ActionResult InvoiceReportViewer()
        {
            return View();
        }

        public ActionResult IDataViewer()
        {
            ReportView rptView = new ReportView();
            rptView.InvoiceList = rptService.GetAllInvoices(invs.GetAllInvoices());
            return View(rptView);
        }

        public ActionResult QuotationReportViewer()
        {
            return View();
        }

        public ActionResult QDataViewer()
        {
            ReportView rptView = new ReportView();
            rptView.QuotationList = rptService.GetAllQuotations(os.GetAllOrders_QuotationRpt());
            return View(rptView);
        }

        public ActionResult WorkOrderReportViewer()
        {
            return View();
        }

        public ActionResult WODataViewer()
        {
            ReportView rptView = new ReportView();
            rptView.WorkOrderList = rptService.GetAllWorkOrders(invs.GetAllInvoices_WORpt());
            return View(rptView);
        }
    }
}
